#!/bin/bash

set -e

# 定义镜像名称和版本
IMAGE_NAME="minidis"
VERSION=${1:-"latest"}

echo "🚀 构建 Minidis Docker 镜像..."

# 构建scratch版本（最小）
echo "📦 构建scratch版本（最小镜像）..."
docker build -t ${IMAGE_NAME}:${VERSION}-scratch -f Dockerfile .

# 构建alpine版本（稳定）
echo "📦 构建alpine版本（稳定镜像）..."
docker build -t ${IMAGE_NAME}:${VERSION}-alpine -f Dockerfile.alpine .

# 创建latest标签指向alpine版本
docker tag ${IMAGE_NAME}:${VERSION}-alpine ${IMAGE_NAME}:${VERSION}
docker tag ${IMAGE_NAME}:${VERSION}-alpine ${IMAGE_NAME}:latest

echo "✅ 构建完成！"
echo ""
echo "📊 镜像大小对比："
docker images ${IMAGE_NAME} --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

echo ""
echo "🏃 运行示例："
echo "  # 运行alpine版本（推荐）："
echo "  docker run -d -p 6379:6379 --name minidis ${IMAGE_NAME}:latest"
echo ""
echo "  # 运行scratch版本（最小）："
echo "  docker run -d -p 6379:6379 --name minidis-scratch ${IMAGE_NAME}:${VERSION}-scratch"
echo ""
echo "  # 测试连接："
echo "  docker run --rm redis:alpine redis-cli -h host.docker.internal -p 6379 ping"